/*global Cesium:true, viewer:true, Cartographic:true*/

// 经纬度转弧度
export let toRadians = (degrees) => {
    return Cesium.Math.toRadians(degrees)
}
// 弧度转经纬度
export let toDegrees = (radians) => {
    return Cesium.Math.toDegrees(radians)
}
// 经纬度转换为世界坐标
export let fromDegrees = (params, viewer, type) => {
    if (type === "radian") {
        let ellipsoid = viewer.scene.globe.ellipsoid;
        let cartographic = Cesium.Cartographic.fromDegrees(...params);
        return ellipsoid.cartographicToCartesian(cartographic);
    }
    return Cesium.Cartesian3.fromDegrees(...params)
}
// 世界坐标转经纬度
export let fromRadians = (params, type) => {
    if (type === "radian") {
        let ellipsoid = viewer.scene.globe.ellipsoid;
        let cartesian3 = new Cesium.cartesian3(...params);
        let cartographic = ellipsoid.cartesianToCartographic(cartesian3);
        let lat = Cesium.Math.toDegrees(cartographic.latitude);
        let lng = Cesium.Math.toDegrees(cartographic.longitude);
        let alt = cartographic.height;
        return [lat, lng, alt]
    }
    return Cartographic.fromCartesian
}
// 图层添加
export let addLayer = (layer, viewer) => {
    return viewer.imageryLayers.add(layer);
}

// 矢量图
export let vector = (params) => ({
    url: "http://ditu.zjzwfw.gov.cn/mapserver/vmap/WMTS/1.0/zjvmap/sanwei_test1?SERICE=WMTS&REQUEST=GetTile&TileMatrixSet=default028mm&VERSION=1.0.0&LAYER=zjvmap&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&format=image/png&height=256&width=256&tilesize=256&ratio=1",
    layer: "",
    style: "",
    format: "",
    tileMatrixSetID: "c",
    subdomains: [],
    tilingScheme: new Cesium.GeographicTilingScheme(),
    tileMatrixLabels: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19"],
    maximumLevel: 18,
    tileWidth: 256,
    tileHeight: 256,
    ...params
})
//   wmts
export let wmts = (params = {}) => ({
    url: 'http://{s}.tianditu.gov.cn/img_w/wmts?service=wmts&request=GetTile&version=1.0.0' + '&LAYER=img&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}' + '&style=default&format=tiles&tk=440b96d736fad95da7934bc130a2258a',
    layer: 'tdtImg_c',
    style: 'default',
    format: 'tiles',
    tileMatrixSetID: 'c',
    subdomains: ['t0', 't1', 't2', 't3', 't4', 't5', 't6', 't7'],
    tilingScheme: new Cesium.WebMercatorTilingScheme(),
    tileMatrixLabels: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19'],
    maximumLevel: 20,
    ...params
})
// 加载天地图底图
export let tianditu = (params = {}) => ({
    url: 'http://t0.tianditu.com/img_w/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=img&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&style=default&format=tiles&tk=55f37d2165bcd500602cfa1188e3905e',
    layer: 'img',
    style: 'default',
    format: 'tiles',
    tileMatrixSetID: 'w',
    credit: new Cesium.Credit('天地图全球影像服务'),
    maximumLevel: 20,
    show: false,
    ...params
})


export let dimension = (ins, orientation) => {
    let type = ins.dimensionType
    let store = {
        '2D': () => {
            ins.viewer.scene.morphTo2D(0)
            ins.viewer.camera.zoomOut(10000)
        },
        '3D': () => {
            ins.viewer.scene.morphTo3D(0)
            ins.camera(orientation)
        }
    }
    if (store[type]) store[type]()
}